package web.report.handler.impl;

import com.yulian.util.RedisConnectionUtil;
import com.yulian.web_api.report.GoodsPriceTrendManager;
import com.yulian.web_api.report.SupplierInvoicingManagementManager;
import com.yulian.web_api.system_management.AuthorizationManager;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.pgclient.PgPool;
import io.vertx.redis.client.Redis;
import io.vertx.redis.client.RedisAPI;
import web.report.handler.GoodsPriceTrendHandler;

public class GoodsPriceTrendHandlerImpl implements GoodsPriceTrendHandler {
    final InternalLogger logger = Log4JLoggerFactory.getInstance(GoodsPriceTrendHandlerImpl.class);
    private final PgPool pool;
    private final JsonObject config;
    private final Redis redis;

    public GoodsPriceTrendHandlerImpl(PgPool pool, JsonObject config, Redis redis) {
        this.pool = pool;
        this.config = config;
        this.redis = redis;
    }

    @Override
    public void handle(RoutingContext req) {
        JsonObject bodyJSObj = req.body().asJsonObject();
        GoodsPriceTrendManager goodsPriceTrendManager = new GoodsPriceTrendManager();
        AuthorizationManager authorization = new AuthorizationManager();
        int server_auth = Integer.parseInt(config.getString("SERVER_AUTH"));
        switch (req.request().uri().toString()) {
            case "/web/goods_price_trend/get_auth":
                //获取当前用户在供应商基本信息页面所具有的按钮权限
                authorization.getAuth(pool, bodyJSObj, req.user()).onSuccess(responseJSObj -> {
                    req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                }).onFailure(err -> {
                    JsonObject responseJSObj = new JsonObject();
                    responseJSObj.put("code", -1).put("msg", "错误: " + err.getMessage());
                    req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                });
                break;
            case "/web/goods_price_trend/get_records":
                if (server_auth == 1) { //需要后端检查用户的权限
                    //通过access_token 获取username,再从redis获取权限字符串数组
                    RedisConnectionUtil.createRedisClient(redis).onSuccess(conn -> {
                        RedisAPI redisApi = RedisAPI.api(conn);
                        String username = req.user().attributes().getJsonObject("accessToken").getString("username");
                        redisApi.get(username).onSuccess(value -> {
                            String permissions = String.valueOf(value);
                            //关闭连接
                            redisApi.close();
                            //conn.close();
                            if (permissions.indexOf("goods_price_trend_get_records") > 0) {
                                goodsPriceTrendManager.getRecords(pool, config, bodyJSObj).onSuccess(responseJSObj -> {
                                    req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                                }).onFailure(err -> {
                                    JsonObject responseJSObj = new JsonObject();
                                    responseJSObj.put("code", -1).put("msg", "错误: " + err.getMessage());
                                    req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                                });
                            } else {
                                JsonObject responseJSObj = new JsonObject();
                                responseJSObj.put("code", -1).put("msg", "错误: 没有权限");
                                req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                            }
                        }).onFailure(err -> {
                            //关闭连接
                            redisApi.close();
                            //conn.close();
                            logger.error(err.toString());
                            JsonObject responseJSObj = new JsonObject();
                            responseJSObj.put("code", -1).put("msg", "读取redis错误:" + err.getCause().getMessage());
                            req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                        });
                    });
                } else { //不检查权限
                    goodsPriceTrendManager.getRecords(pool, config, bodyJSObj).onSuccess(responseJSObj -> {
                        req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                    }).onFailure(err -> {
                        JsonObject responseJSObj = new JsonObject();
                        responseJSObj.put("code", -1).put("msg", "错误: " + err.getMessage());
                        req.response().putHeader("content-type", "application/json").end(responseJSObj.toString());
                    });
                }
                break;
        }
    }
}
